<template>
	<view class="homeLayout pageBg">
		<custom-nav-bar title="推荐"></custom-nav-bar>
		<view class="banner">
			<swiper cycle indicator-color="#999" indicator-active-color="#fff" :indicator-dots="true" :autoplay="true"
				:interval="3000" :duration="1000">
				<swiper-item v-for="item in bannerList" :key="item._id">
					<!-- 跳转到其他小程序 -->
					<navigator v-if="item.target === 'miniProgram'" :url="item.url" class="like" target="miniProgram"
						:app-id="item.appid">
						<image :src="item.picurl" mode="aspectFill"></image>
					</navigator>

					<navigator v-else :url="`/pages/classlist/classlist?${item.url}`" class="like">
						<image :src="item.picurl" mode="aspectFill"></image>
					</navigator>
				</swiper-item>
			</swiper>
		</view>

		<view class="notice">
			<view class="left">
				<uni-icons type="sound-filled" size="28"></uni-icons>
				<text class="text">公告</text>
			</view>
			<view class="center">
				<!-- 垂直轮播 -->
				<swiper :vertical="true" cycle :autoplay="true" :interval="3000" :duration="1000">
					<swiper-item v-for="item in noticeList" :key="item._id">
						<navigator :url="'/pages/notice/detail?id='+item._id">
							{{item.title}}
						</navigator>
					</swiper-item>
				</swiper>
			</view>
			<view class="right">
				<uni-icons type="right" size="28" color="#9c9c9c"></uni-icons>
			</view>
		</view>

		<view class="select">
			<common-title>
				<template #title>
					每日推荐
				</template>
				<template #custom>
					<view class="data">
						<uni-icons type="calendar" size="20"></uni-icons>
						<uni-dateformat :date="Date.now()" format="dd日"></uni-dateformat>
					</view>
				</template>
			</common-title>
			<scroll-view class="content" :scroll-x="true">
				<view class="box" v-for="item in randomList" :key="item.id" @click="goPreview">
					<image :src="item.smallPicurl" mode="aspectFill"></image>
				</view>
			</scroll-view>
		</view>

		<view class="theme">
			<common-title>
				<template #title>
					专题精选
				</template>
				<template #custom>
					<navigator url="/pages/classify/classify" open-type="reLaunch" class="more">More+</navigator>
				</template>
			</common-title>
			<view class="content">
				<theme-item v-for="item in classifyList" :key="item._id" :item="item"></theme-item>
				<theme-item :isMore="true"></theme-item>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue';
	import {
		getBannerAPI,
		getDayRandomAPI,
		getNoticeAPI,
		getClassifyAPI
	} from '@/api/api.js';
	import {
		onShareTimeline,
		onShareAppMessage
	} from '@dcloudio/uni-app';
	const bannerList = ref([]);
	const randomList = ref([]);
	const noticeList = ref([]);
	const classifyList = ref([]);

	const getBanner = async () => {
		let res = await getBannerAPI();
		bannerList.value = res.data;
	}
	const getDayRandom = async () => {
		let res = await getDayRandomAPI();
		randomList.value = res.data
	}

	const goPreview = (id) => {
		uni.setStorageSync('storeClassList', randomList.value);
		uni.navigateTo({
			url: '/pages/preview/preview?id=' + id
		})
	}

	const getNotice = async () => {
		let res = await getNoticeAPI({
			select: true
		});
		noticeList.value = res.data
	}

	const getClassify = async () => {
		let res = await getClassifyAPI({
			select: true
		});
		classifyList.value = res.data
	}
	onMounted(() => {
		getBanner();
		getDayRandom();
		getNotice();
		getClassify();
	})

	//分享给好友
	onShareAppMessage((e) => {
		return {
			title: "咸虾米壁纸，好看的手机壁纸",
			path: "/pages/classify/classify"
		}
	})

	//分享朋友圈
	onShareTimeline(() => {
		return {
			title: "咸虾米壁纸，好看的手机壁纸",
			imageUrl: '@/static/logo.png'
		}
	})
</script>

<style lang="scss" scoped>
	.homeLayout {
		.banner {
			width: 750rpx;
			padding: 30rpx 0;

			swiper {
				width: 750rpx;
				height: 340rpx;

				swiper-item {
					height: 100%;
					width: 100%;
					padding: 0 30rpx;

					.like: {
						height: 100%;
						width: 100%;

						image {
							height: 100%;
							width: 100%;
							border-radius: 10rpx;
						}

					}

				}
			}
		}

		.notice {
			width: 690rpx;
			height: 80rpx;
			border-radius: 80rpx;
			background-color: #f9f9f9;
			margin: 0 auto;
			display: flex;
			align-items: center;
			line-height: 80rpx;

			.left {
				width: 140rpx;
				display: flex;
				align-items: center;

				// H5不用deep，而小程序需要deep穿透组件
				:deep() {
					.uni-icons {
						color: $brand-theme-color !important;
					}
				}

				.text {
					font-weight: 600;
					font-size: 30rpx;
					color: $brand-theme-color;
				}
			}

			.center {
				flex: 1;

				swiper {
					height: 80rpx;

					swiper-item {
						height: 100%;
						font-size: 25rpx;
						color: #666;
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
					}
				}
			}

			.right {
				width: 70rpx;
			}
		}

		.select {
			padding-top: 40rpx;

			.data {
				display: flex;
				align-items: center;
			}

			.content {
				width: 720rpx;
				margin-top: 30rpx;
				margin-left: 30rpx;
				white-space: nowrap;

				.box {
					width: 200rpx;
					height: 430rpx;
					display: inline-block;
					margin-right: 15rpx;

					image {
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
				}

				.box:last-child {
					margin-right: 30rpx;
				}
			}
		}

		.theme {
			padding: 50rpx 0;

			.more {
				font-size: 35rpx;
				color: #888;
			}

			.content {
				margin: 30rpx 30rpx 0;
				display: grid;
				gap: 15rpx;
				grid-template-columns: repeat(3, 1fr);
			}
		}
	}
</style>